package com.example.elmmerchants;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.elmmerchants.First_Page_Fragment.Order_Page;
import com.example.elmmerchants.Order_Setting.Order_Setting_Page;
import com.example.elmmerchants.Ringtone_Setting.Ringtone_Setting_Page;
import com.example.elmmerchants.SQL.Connection_SQL;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Auto_Get_Order_Page extends AppCompatActivity {

    RelativeLayout open_order_ringtone;
    AlertDialog dialog;
    String email;
    Button open_auto_get_order;
    boolean is_open_auto_get_order, is_finish_loading_ringtone_data = false;
    boolean is_open_vibrate = false;
    boolean is_upload_data = false;
    int book_almost_time, book_has_ride_get_order, new_order, auto_get_order, reminder_order, order_cancel, user_apply_refund, abnormal_distribution, how_many_volume;

    private class Open_Or_Close_Auto_Get_Order_Thread extends Thread {
        @Override
        public void run() {
            is_upload_data = false;

            ExecutorService executorService = Executors.newSingleThreadExecutor();
            executorService.execute(() -> {
                try {
                    Connection_SQL userSQL = new Connection_SQL();
                    Connection connection = userSQL.connection();
                    String sql = "update `merchant` set is_open_auto = ? where email = ?";
                    PreparedStatement preparedStatement = connection.prepareStatement(sql);
                    if (is_open_auto_get_order) {
                        preparedStatement.setInt(1, 0);
                    }
                    else {
                        preparedStatement.setInt(1, 1);
                    }
                    preparedStatement.setString(2, email);
                    preparedStatement.executeUpdate();
                    is_upload_data = true;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

            while (!is_upload_data) {
                try {
                    Thread.sleep(1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (is_open_auto_get_order) {
                        Toast.makeText(Auto_Get_Order_Page.this, "已关闭自动接单", Toast.LENGTH_SHORT).show();
                    }
                    else {
                        Toast.makeText(Auto_Get_Order_Page.this, "已开启自动接单", Toast.LENGTH_SHORT).show();
                    }
                    Intent intent = new Intent(Auto_Get_Order_Page.this, Order_Page.class);
                    intent.putExtra("is_open", is_open_auto_get_order);
                    setResult(1, intent);
                    finish();
                }
            });
        }
    }

    private class Loading_Ringtone_Data_Thread extends Thread {
        @Override
        public void run() {
            is_finish_loading_ringtone_data = false;

            ExecutorService executorService = Executors.newSingleThreadExecutor();
            executorService.execute(() -> {
                try {
                    Connection_SQL userSQL = new Connection_SQL();
                    Connection connection = userSQL.connection();
                    String sql = "SELECT * FROM merchant_ringtone_setting WHERE email = ?;";
                    PreparedStatement preparedStatement = connection.prepareStatement(sql);
                    preparedStatement.setString(1, email);
                    ResultSet set = preparedStatement.executeQuery();
                    while (set.next()) {
                        book_almost_time = set.getInt("book_almost_time");
                        book_has_ride_get_order = set.getInt("book_has_ride_get_order");
                        new_order = set.getInt("new_order");
                        auto_get_order = set.getInt("auto_order");
                        reminder_order = set.getInt("reminder_order");
                        order_cancel = set.getInt("order_cancel");
                        user_apply_refund = set.getInt("user_apply_refund");
                        abnormal_distribution = set.getInt("abnormal_distribution");
                        how_many_volume = set.getInt("volume");
                        if (set.getInt("is_open_vibrate") == 1) {
                            is_open_vibrate = true;
                        }
                        else {
                            is_open_vibrate = false;
                        }
                        is_finish_loading_ringtone_data = true;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

            while (!is_finish_loading_ringtone_data) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    dialog.dismiss();
                    Intent intent = new Intent(Auto_Get_Order_Page.this, Ringtone_Setting_Page.class);
                    intent.putExtra("email", email);
                    intent.putExtra("book_almost_time", book_almost_time);
                    intent.putExtra("book_has_ride_get_order", book_has_ride_get_order);
                    intent.putExtra("new_order", new_order);
                    intent.putExtra("auto_get_order", auto_get_order);
                    intent.putExtra("reminder_order", reminder_order);
                    intent.putExtra("order_cancel", order_cancel);
                    intent.putExtra("user_apply_refund", user_apply_refund);
                    intent.putExtra("abnormal_distribution", abnormal_distribution);
                    intent.putExtra("how_many_volume", how_many_volume);
                    intent.putExtra("is_open_vibrate", is_open_vibrate);
                    startActivityForResult(intent, 2);
                }
            });
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_auto_get_order_page);
        email = getIntent().getStringExtra("email");
        is_open_auto_get_order = getIntent().getBooleanExtra("is_open_auto_get_order", false);

        open_order_ringtone = findViewById(R.id.open_order_ringtone);
        open_auto_get_order = findViewById(R.id.open_auto_get_order);

        if (is_open_auto_get_order) {
            open_auto_get_order.setText("关闭自动接单");
        }
        else {
            open_auto_get_order.setText("开启自动接单");
        }

        open_order_ringtone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                set_dialog();
                Loading_Ringtone_Data_Thread loadingRingtoneDataThread = new Loading_Ringtone_Data_Thread();
                loadingRingtoneDataThread.start();
            }
        });

        open_auto_get_order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Open_Or_Close_Auto_Get_Order_Thread orderThread = new Open_Or_Close_Auto_Get_Order_Thread();
                orderThread.start();
            }
        });
    }

    public void set_dialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        LayoutInflater inflater1 = LayoutInflater.from(this);
        builder.setView(inflater1.inflate(R.layout.loading, null));
        builder.setCancelable(false);

        dialog = builder.create();
        dialog.show();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case 2: {
                //Loading_Ringtone_Data_Thread loadingRingtoneDataThread = new Loading_Ringtone_Data_Thread();
                //loadingRingtoneDataThread.start();
                break;
            }
        }
    }
}